package com.zxx.googlemap;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * 因为ViewPager2内部封装的是RecyclerView，
 * 因此它的Adapter也就是RecyclerView的Adapter。
 *
 * @author zqq on 2020/2/27.
 */
public class ViewPager2Adapter extends RecyclerView.Adapter<ViewPager2Adapter.ViewHolder> {

    private List<Integer> mList = new ArrayList<>();

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pager, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bindData(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    //
    public void setList(List<Integer> list) {
        this.mList = list;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        private String[] colors = new String[]{"#CCFF99", "#41F1E5", "#8D41F1", "#FF99CC"};

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_text);
        }

        public void bindData(int i) {
            textView.setText(String.valueOf(i));
            textView.setBackgroundColor(Color.parseColor(colors[i]));
        }
    }


}
